package com.holystone.orca.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.holystone.orca.common.JSONMapper;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.common.SystemConstant;
import com.holystone.orca.sys.mybatis.model.SysLookupType;
import com.holystone.orca.sys.service.ISysLookupCodeService;
import com.holystone.orca.sys.service.ISysLookupTypeService;

/**
 * Copyright (c) 2015, HOLYSTONE Technologies, Inc.
 * All right reserved.
 * @desc: 码表类型
 * @author:fengt
 * @date:15-11-17
 */
@Controller
@RequestMapping(value = "/sys/lookup_management")
public class SysLookupTypeController {

    private static Logger logger = Logger.getLogger(SysLookupTypeController.class);

    @Resource(name="sysLookupCodeService")
    private ISysLookupCodeService sysLookupCodeService;
    @Resource(name="sysLookupTypeService")
    private ISysLookupTypeService sysLookupTypeService;

    /**
     * @author fent
     * @desc  码表类型表初始化页面
     * @param iPageNo
     * @param iPageSize
     * @return ModelAndView
     * @returntype jsp
     */
    @RequestMapping(value="/initlookuptype")
    public ModelAndView initBase( String  iPageNo, String iPageSize){
        int pageindex = iPageNo!=null?Integer.parseInt(iPageNo): SystemConstant.default_pageNum;
        int pagesize = iPageSize!=null?Integer.parseInt(iPageSize):SystemConstant.default_pageSize;
        ModelAndView model = new ModelAndView("sys/lookup_management/lookupType_management");
        try{
            PageData pageData = sysLookupTypeService.pagingSysLookupType(pageindex, pagesize);
            model.addObject("data",pageData);
        }catch(Exception e){
            logger.error(e,e);
        }
        return model;
    }

    /**
     * 获取分页码表类型
     * @param iPageNo
     * @param iPageSize
     * @return ModelMap 转json
     * @returntype json
     */
    @ResponseBody
    @RequestMapping(value="/pagingLookupType")
    public ModelMap pagingLookup(String iPageNo, String iPageSize){
        int pageindex = iPageNo!=null?Integer.parseInt(iPageNo):SystemConstant.default_pageNum;
        int pagesize = iPageSize!=null?Integer.parseInt(iPageSize):SystemConstant.default_pageSize;
        PageData pageData =null;
        try{
            pageData= sysLookupTypeService.pagingSysLookupType(pageindex, pagesize);
        }catch(Exception e){
            logger.error(e,e);
        }
        return JSONMapper.createReturnMap(null, pageData, null);
    }
    /**
     * 保存码表类型
     * @param  sysLookupType
     * @return ModelMap 转json
     * @returntype json
     */
    @ResponseBody
    @RequestMapping(value="/saveLookupType")
    public ModelMap saveLookupType(SysLookupType sysLookupType,String type){
        String errorMessage = "success";
        Map map = new HashMap();
        if(sysLookupType!=null && ! SystemConstant.isNull(sysLookupType.getLookupType()) && !SystemConstant.isNull(sysLookupType.getLookupDesc())){
            try{
                int result =sysLookupTypeService.saveOrUpdateLookupType(sysLookupType,type);
                map.put("flag",result);
            }catch(Exception e){
                logger.error(e,e);
                errorMessage="error";
            }
        }
        return JSONMapper.createReturnMap(errorMessage, null, map);
    }

}
